package com.iap.app.util
{
	import com.iap.app.util.logger.Log;
	
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	
	import mx.collections.ArrayCollection;
	import mx.controls.ColorPicker;
	import mx.controls.DateField;
	import mx.utils.StringUtil;
	
	import spark.components.CheckBox;
	import spark.components.ComboBox;
	import spark.components.Label;
	import spark.components.NumericStepper;
	import spark.components.RadioButtonGroup;
	import spark.components.TextArea;
	import spark.components.TextInput;

	public class FormUtils
	{
		private static var sourceXMLAccessor:XMLList = null;

		public function FormUtils()
		{
		}

		public static function eval2Form(value:Object):String
		{
			return (value!=null)?new String(value):"";
		}

		private static function controlExist(controlName:String,type:String):Boolean
		{
			return sourceXMLAccessor.(@name==controlName&&@type==type).length()>0;
		}

		public static function fillFormControl(sourceControl:Object, data:Object, prefixName:String = ""):void
		{
			sourceXMLAccessor = describeType(sourceControl)..accessor;
			for each(var key:String in describeType(data)..accessor.@name)
			{
				if(data[key] != null)
				{
					var keyForm:String = key+prefixName;
					if(controlExist(keyForm, getQualifiedClassName(TextInput)))
					{
						(sourceControl[keyForm] as TextInput).text = eval2Form(data[key]);
					}
					if(controlExist(keyForm,getQualifiedClassName(Label)))
					{
						(sourceControl[keyForm] as Label).text = eval2Form(data[key]);
					}
					if(controlExist(keyForm,getQualifiedClassName(TextArea)))
					{
						(sourceControl[keyForm] as TextArea).text = eval2Form(data[key]);
					}
					if(controlExist(keyForm,getQualifiedClassName(NumericStepper)))
					{
						(sourceControl[keyForm] as NumericStepper).value = data[key];
					}
					if(controlExist(keyForm,getQualifiedClassName(DateField)))
					{
						if((data[key] as Date).fullYear>1900){
							(sourceControl[keyForm] as DateField).data = data[key];
						}
					}
					if(controlExist(keyForm,getQualifiedClassName(ComboBox)))
					{
						var list:ArrayCollection = (sourceControl[keyForm] as ComboBox).dataProvider as ArrayCollection;
						try {
							for(var i:Number = 0; i<list.length; i++)
							{
								if(String(list[i].data).toUpperCase() == data[key].toString().toUpperCase())
								{
									(sourceControl[keyForm] as ComboBox).selectedIndex = i;
								}
							}
						} catch(err:Error)
						{
							Log.Error("FormUtils.Error : "+ err.message);
						}
					}
					if(controlExist(keyForm,getQualifiedClassName(CheckBox)))
					{
						(sourceControl[keyForm] as CheckBox).selected = (data[key] as Boolean);
					}
					if(controlExist(keyForm,getQualifiedClassName(ColorPicker)))
					{
						(sourceControl[keyForm] as ColorPicker).selectedColor = data[key];
					}
					if(controlExist(keyForm,getQualifiedClassName(RadioButtonGroup)))
					{
						(sourceControl[keyForm] as RadioButtonGroup).selectedValue = data[key];
					}
				}
			}
		}

		public static function fillObjectFromControls(sourceControl:Object, data:Object, prefixName:String = ""):*
		{
			sourceXMLAccessor = describeType(sourceControl)..accessor;
			for each(var key:String in describeType(data)..accessor.@name)
			{
				var keyForm:String = key+prefixName;
				if(controlExist(keyForm,getQualifiedClassName(TextInput)))
				{
					if((sourceControl[keyForm] as TextInput).enabled){
						data[key] = StringUtil.trim((sourceControl[keyForm] as TextInput).text);
					}
				}
				if(controlExist(keyForm,getQualifiedClassName(TextArea)))
				{
					if((sourceControl[keyForm] as TextArea).enabled){
						data[key] = StringUtil.trim((sourceControl[keyForm] as TextArea).text);
					}
				}
				if(controlExist(keyForm,getQualifiedClassName(DateField)))
				{
					data[key] = (sourceControl[keyForm] as DateField).selectedDate;
					if(data[key]==null)
					{
						data[key] = new Date(0,0,0);
					}
				}
				if(controlExist(keyForm,getQualifiedClassName(NumericStepper)))
				{
					data[key] = (sourceControl[keyForm] as NumericStepper).value;
				}
				if(controlExist(keyForm,getQualifiedClassName(ComboBox)))
				{
					var comboBox:ComboBox = (sourceControl[keyForm] as ComboBox);
					try {
						data[key] = comboBox.selectedItem.data;
					} catch(err:Error)
					{
						data[key] = comboBox.selectedItem;
					}
				}
				if(controlExist(keyForm,getQualifiedClassName(CheckBox)))
				{
					data[key] = (sourceControl[keyForm] as CheckBox).selected;
				}
				if(controlExist(keyForm,getQualifiedClassName(ColorPicker)))
				{
					data[key] = ColorUtil.hexToString((sourceControl[keyForm] as ColorPicker).selectedColor);
				}
				if(controlExist(keyForm,getQualifiedClassName(RadioButtonGroup)))
				{
					data[key] = (sourceControl[keyForm] as RadioButtonGroup).selectedValue;
				}
			}
			return data;
		}

		public static function focusControlLast(source:TextInput):void
		{
			source.setFocus();
		}

		public static function setComboBox(source:ComboBox,data:Object,propertyName:String):void
		{
			var list:ArrayCollection = source.dataProvider as ArrayCollection;
			try {
				for(var i:Number = 0; i<list.length; i++)
				{
					if(list[i][propertyName].toString().toUpperCase()==data[propertyName].toString().toUpperCase())
					{
						source.selectedIndex = i;
					}
				}
			} catch(err:Error)
			{
				Log.Error("FormUtils.setComboBox.Error : "+ err.message);
			}
		}

		public static function isControlExtended(source:Object,control:Object):Boolean
		{
			var ret:Boolean = describeType(source)..extendsClass
				.(@type.indexOf(getQualifiedClassName(control))>-1).length()>0;
			return ret;
		}
	}
}